package com.culture.config.common.util;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.culture.config.common.model.PageInfo;
import com.culture.config.common.model.ResponseData;
import com.culture.config.common.model.ResponsePageData;
import com.github.pagehelper.Page;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@UtilityClass
public class ResponseUtil {	
	
	public static <T> ResponseData<T> success(T data) {
		ResponseData<T> res = new ResponseData<>(ResponseData.CODE_SUCCESS,ResponseData.MSG_SUCCESS);
		res.setData(data);
		return res;
	}
	
	public static <T> ResponsePageData<List<T>> successPage(Page<T> data) {		
		ResponsePageData<List<T>> res = new ResponsePageData<>(ResponseData.CODE_SUCCESS,ResponseData.MSG_SUCCESS);	
		if(Objects.isNull(data)) {			
			return res.setPageInfo(new PageInfo());
		}
		log.info("successPage input data:{}",data);
		res.setPageInfo(new PageInfo(data.getTotal(),data.getPageSize(),data.getPageNum(),data.getPages()));
		res.setData(data.getResult().stream().collect(Collectors.toList()));
		log.info("successPage output data:{}",res);
		return res;
	}
	
	public static ResponseData<String> success() {
		return new ResponseData<>(ResponseData.CODE_SUCCESS, ResponseData.MSG_SUCCESS);
	}
	
	public static ResponseData<String> ok() {
		return new ResponseData<>(ResponseData.CODE_SUCCESS, ResponseData.MSG_OK);
	}
}
